package wechat.base.snd

//import org.eclipse.xtend.lib.Property
import org.json.JSONObject
import wechat.base.snd.encoding.JsonPut
import wechat.base.snd.util.ExecPost

//import static wechat.base.snd.ContactManager.*
import org.eclipse.xtend.lib.annotations.Accessors

class ContactManager {
	static String WECHAT_CONTACTS_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=";
	private static ContactManager customerManager = null
	private WCApplication _corpApp
	
	def static getContactManager(WCApplication corpApp){
		new ContactManager(corpApp)
	}
	new (WCApplication corpApp){
		if(customerManager == null){
			customerManager = new ContactManager(corpApp)
			_corpApp = corpApp
		}
	}	
	def getContactUrl(){
		WECHAT_CONTACTS_URL+ _corpApp.token
	}
	def ErrorMsg addContact(Contact c){
		new ErrorMsg(new JSONObject(ExecPost::excuteJsonPost(contactUrl, c.jsonBuilder.toString)))
	}
}
class Contact{
	@Accessors String userid;
	@Accessors String name;
	@Accessors String department;
	@Accessors String position;
	@Accessors String mobile;
	@Accessors String gender;
	@Accessors String email;
 	@Accessors String weixinid;
	@Accessors String avatar_mediaid;
	@Accessors String extattr;
	new (String weixinid, String name){
		this.name = name
		this.weixinid = weixinid
		this.userid = weixinid
	}
	def JSONObject jsonBuilder(){
		val jo = new JsonPut()=>[
			putObj('userid',userid)
			putObj('name',name)
			putObj('department',department)
			putObj('position',position)
			putObj('mobile',mobile)
			putObj('gender',gender)
			putObj('email',email)
			putObj('weixinid',weixinid)
			putObj('avatar_mediaid',avatar_mediaid)
			putObj('extattr',extattr)
		]
		jo
	}
}